﻿Public Class XLHopDong
    Dim data_ As New DataTable
    Dim ds As ArrayList
    Dim cv As New KNChucVu
    Dim qt As New KNQuocTich
    Dim kn As New KNHopDong
    Dim ts As New KNThamSo

    Public Function LoaiHD() As ArrayList
        ds = New ArrayList
        Dim t As String = "[Chọn]"
        ds.Add(t)
        t = "Không Xác Định Thời Hạn"
        ds.Add(t)
        t = "Xác Định Thời Hạn"
        ds.Add(t)
        t = "Theo Mùa Vụ"
        ds.Add(t)
        Return ds
    End Function
    Public Function BUS_HTTra() As ArrayList
        ds = New ArrayList
        Dim t As String = "[Chọn]"
        ds.Add(t)
        t = "Chuyển Khoản"
        ds.Add(t)
        t = "Tiền Mặt"
        ds.Add(t)
        Return ds
    End Function
    Public Function BUS_THHD() As ArrayList
        ds = New ArrayList
        Dim t As String = "[Chọn]"
        ds.Add(t)
        t = "6 Tháng"
        ds.Add(t)
        t = "1 Năm"
        ds.Add(t)
        t = "3 Năm"
        ds.Add(t)
        t = "5 Năm"
        ds.Add(t)
        Return ds
    End Function
    Public Function BUS_DHDDK(ByVal hd As String) As DataTable
        Return kn.DKDSHD(hd)
    End Function
    Public Function BUS_DSCTHD() As DataTable
        Return kn.DSCTHD()
    End Function
    Public Function DSCV() As ArrayList
        ds = New ArrayList
        Dim c As New DTO_ChucVu
        c.TenChucVu = "[Chọn]"
        ds.Add(c)
        data_ = cv.DSCV()
        If data_.Rows.Count < 0 Then Return ds
        For Each dr As DataRow In data_.Rows
            c = New DTO_ChucVu
            c.TenChucVu = dr("TenCV")
            ds.Add(c)
        Next
        Return ds
    End Function

    Public Function DSQT() As ArrayList
        ds = New ArrayList
        data_ = qt.DSQT()
        If data_.Rows.Count < 0 Then Return ds
        For Each dr As DataRow In data_.Rows
            Dim q As New DTO_QuocTich
            q.TenNuoc = dr("TenNuoc")
            ds.Add(q)
        Next
        Return ds
    End Function
    Public Function BUS_ArrDSCTHD(ByRef data As DataTable) As ArrayList
        Dim ds As New ArrayList
        'data_ = New DataTable
        data = kn.DSCTHD()
        For Each dr As DataRow In data.Rows
            Dim h As New DTO_HopDong.HopDong
            h.MaHD = dr("MaHD")
            h.MaNV = dr("MaNV")
            ds.Add(h)
        Next
        Return ds
    End Function
    Public Function BUS_LayMaNV(ByVal hd As String) As String
        data_ = kn.MAnv_CTHD(hd)
        'Return data_.Rows(0).Item("MaNV").ToString()
        Dim kq As String = ""
        For Each dr As DataRow In data_.Rows
            kq = dr("MaNV")
        Next
        Return kq
    End Function
    Public Function BUS_AllDSHD() As DataTable
        Return kn.DSHD
    End Function

    Public Function BUS_SaveHD1(ByVal data As DataTable, ByVal dataCT As DataTable) As Boolean
        kn.SaveHD(data)
        Return kn.SaveHDCT(dataCT)
    End Function
    Public Function BUS_SaveHD(ByVal data As DataTable) As Boolean
        Return kn.SaveHD(data)
    End Function
    Public Function BUS_SaveHDCT(ByVal data As DataTable) As Boolean
        Return kn.SaveHDCT(data)
    End Function
    Public Function LayDSHD() As DataTable
        Return kn.DSHD()
    End Function
    Public Function BUS_MaHD(ByVal str As String) As String
        Dim kq As String = String.Empty
        data_ = kn.MaHD_CTHD(str)
        For Each dr As DataRow In data_.Rows
            kq = dr("MaHD")
        Next
        Return kq
    End Function
    Public Function BUS_ArrDSLC(ByRef pb As List(Of DTO_PhongBan.PhongBan)) As ArrayList
        Dim ds As New ArrayList
        Dim k As New XLPhongBan
        Dim str As String = "Tất cả"
        ds.Add(str)
        pb = k.BUS_LayDSPB
        For Each p As DTO_PhongBan.PhongBan In pb
            ds.Add(p.TenPhong)
        Next
        Return ds
    End Function
    Public Sub BUS_DKDSHD(ByRef arrnv As ArrayList, ByRef dataCT As DataTable, ByVal str As String, ByVal pb As List(Of DTO_PhongBan.PhongBan))
        dataCT = New DataTable
        For Each p As DTO_PhongBan.PhongBan In pb
            If p.TenPhong = str Then ' neu = ten phong
                Dim nv As New KNNhanVien
                data_ = nv.DKDSNV(p.MaPB)
                If data_.Rows.Count <> 0 Then
                    For Each dr As DataRow In data_.Rows
                        Dim n As New DTO_NhanVienCT.NhanVien
                        n.MaNV = dr("MaNV")
                        n.HoTen = dr("HoTen")
                        arrnv.Add(n)
                        Dim dataHD As New DataTable
                        dataHD = kn.MaHD_CTHD(n.MaNV)
                        If dataHD.Rows.Count <> 0 Then
                            For Each dr1 As DataRow In dataHD.Rows
                                dataCT = kn.DKDSHD(dr1("MaHD"))
                            Next
                        End If
                    Next
                End If
            End If
        Next
    End Sub
    Public Function LayLuongCB() As String
        Dim lcb As Double = 0
        Dim min As Double = 0
        data_ = ts.LuongCB()
        If data_.Rows.Count < 0 Then Return lcb
        For Each dr As DataRow In data_.Rows
            If dr("LuongCB") > min Then
                min = dr("LuongCB")
            End If
        Next
        lcb = min
        Return lcb
    End Function
    Public Function BUS_NgTrLuong() As String
        Dim ngtra As Integer = 15
        data_ = ts.LuongCB()
        If data_.Rows.Count < 0 Then Return ngtra
        For Each dr As DataRow In data_.Rows
            ngtra = dr("NgTra")
        Next
        Return ngtra
    End Function
    Public Function KtraKyHD(ByVal manv As String, ByVal dsct As ArrayList) As Boolean
        Dim kq As Boolean = False
        For Each ct As DTO_HopDong.HopDong In dsct
            If ct.MaNV = manv Then
                kq = True
                Exit For
            End If
        Next
        Return kq
    End Function
    Public Function BUS_KtraKTHD(ByVal str As String, ByVal str1 As String) As Boolean
        Dim kq As Boolean = False
        Dim arr As String() = str.Split("/")
        Dim arr1 As String() = str1.Split("/")
        If (Convert.ToUInt32(arr1(1).ToString) - Convert.ToUInt32(arr(1).ToString)) = 1 Then
            kq = True
        End If
        Return kq
    End Function
    Public Function BUS_ListDSNV(ByVal manv As String) As List(Of DTO_NhanVienCT.NhanVien)
        Dim kn As New KNNhanVien
        Return kn.DSNVDK(manv)
    End Function
    Public Function BUS_MAHD() As String
        Dim kq = "HD0000000"
        data_ = kn.DSCTHD
        If data_.Rows.Count <> 0 Then
            Dim i As Integer = data_.Rows.Count
            Dim mahd As String = data_.Rows(i - 1).Item("MaHD").Substring(0, 2)
            Dim d As String = data_.Rows(i - 1).Item("MaHD").Substring(2)
            Dim n As Integer = Convert.ToUInt32(d) + 1
            d = "00000000" & n
            If d.Length > 8 Then
                For j As Integer = 1 To d.Length
                    d = d.Substring(1)
                    If d.Length = 8 Then
                        Exit For
                    End If
                Next
            End If
            kq = mahd & d
        End If
        Return kq
    End Function
    Public Function BUS_KtraThem(ByVal h As DTO_HopDong.HopDong, ByVal datact As DataTable, ByVal dau As Integer) As Boolean
        Dim kq As Boolean = False
        If dau = 1 Then
            For Each dr As DataRow In datact.Rows
                If h.MaNV = dr("MaNV") Or h.MaHD = dr("MaHD") Then
                    kq = True
                    Exit For
                End If
            Next
        End If
        If h.THan = "" Then kq = True
        If h.TTrang = False Then kq = True
        If h.GLam = "" Then kq = True
        If h.BHiem = "" Then kq = True
        Return kq
    End Function
    Public Sub BUS_ADD_UPHD(ByRef dr As DataRow, ByRef dl As DataRow, ByVal h As DTO_HopDong.HopDong)
        dr("MaHD") = h.MaHD
        dr("MaNV") = h.MaNVDD
        dr("NNghiep") = h.NNghiep
        dr("SoLD") = h.SoLD
        dr("NgCap") = h.NgCap
        dr("NCap") = h.NCap
        dr("LoaiHD") = h.TenHD
        dr("THan") = h.THan
        dr("NgKy") = h.NgKy
        dr("NgBD") = h.NgHLuc
        dr("NgKT") = h.NgKT
        dr("NgBDTV") = h.NgBDTV
        dr("NgKTTV") = h.NgKTTV
        dr("CDanh") = h.CDanh
        dr("CVu") = h.Cvu
        dr("CViec") = h.CViec
        dr("DCLam") = h.DCLam
        dr("LuongCB") = h.LCB
        dr("TrLuong") = h.TrLuong
        dr("NgTra") = h.NgTra
        dr("PCap") = h.PCap
        dr("BHiem") = h.BHiem
        dr("GLam") = h.GLam
        dr("TrYeu") = h.TrYeu
        dr("GChu") = h.GChu
        dr("TTrang") = h.TTrang
        dl("MaHD") = h.MaHD
        dl("MaNV") = h.MaNV
    End Sub
    Public Function BUS_LCBHD(ByVal manv As String) As Double
        data_ = New DataTable
        Dim kq As Double
        data_ = kn.LCB_HD(manv)
        For Each dr As DataRow In data_.Rows
            kq = dr("LuongCB")
        Next
        Return kq
    End Function
    Public Sub BUS_PCHD(ByVal manv As String, ByRef tepc As String, ByRef stien As Double)
        data_ = New DataTable
        data_ = kn.LCB_HD(manv)
        For Each dr As DataRow In data_.Rows
            tepc = dr("PCap")
        Next
        stien = TimPC(tepc)
    End Sub
    Public Function TimPC(ByVal pc As String) As Double
        Dim kq As Double = 0
        Dim kn As New KNPhuCap
        Dim dat As New DataTable
        dat = kn.DSPC()
        Dim arr As String() = pc.Split(",")
        For i As Integer = 0 To arr.Count - 1
            For Each dr As DataRow In dat.Rows
                If dr("TenPC") = arr(i).ToString() Then
                    kq += Convert.ToDouble(dr("SoTien"))
                    Exit For
                End If
            Next
        Next
        Return kq
    End Function
End Class
